import {
  createParams,
  detailItem,
  detailParam,
  editParams,
  linkItem,
  linkParams,
  workLoadItem,
  workLoadParam,
} from './model/linkManagementModel';

import { upgradeHttp } from '/@/utils/http/axios';

enum Api {
  GET_LINK_TABLE = '/v1/routeLink/list',
  GET_NAMESPACE_LIST = '/v1/routeLink/allNamespace',
  CREATE_LINK = '/v1/routeLink/create',
  EDIT_LINK = '/v1/routeLink/edit',
  LINK_DETAIL = '/v1/routeLink/details',
  DELETE_LINK = '/v1/routeLink/delete',
  GET_WORKLOAD = '/v1/routeLink/getAllServiceInNamespace',
}

//获取规则表格
export const getLinkTable = async (params: linkParams) => {
  const res = await upgradeHttp.get<linkItem[]>({
    url: Api.GET_LINK_TABLE,
    params,
  });
  return res;
};
//获取命名空间
export const getNamespaceList = async () => {
  const res = await upgradeHttp.get<string[]>({
    url: Api.GET_NAMESPACE_LIST,
  });
  const result = res.map((item) => {
    return {
      label: item,
      value: item,
    };
  });
  return result;
};
//获取选择的命名空间下的工作负载
export const getWorkLoad = async (params: workLoadParam[]) => {
  const res = await upgradeHttp.post<workLoadItem[]>({
    url: Api.GET_WORKLOAD,
    params,
  });
  res.map((item) => {
    if (item.deployment == '--') {
      item.isRun = '否';
    } else {
      item.isRun = '是';
    }
    return item;
  });
  return res;
};
//创建链路
export const createLink = async (params: createParams) => {
  const res = await upgradeHttp.post<string>({
    url: Api.CREATE_LINK,
    params,
  });

  return res;
};
//编辑规则
export const editLink = async (params: editParams) => {
  const res = await upgradeHttp.put<string>({
    url: Api.EDIT_LINK,
    params,
  });
  return res;
};
//查看详情
export const getLinkDetail = async (params: detailParam) => {
  const _params = { id: params };
  const res = await upgradeHttp.get<detailItem>({
    url: Api.LINK_DETAIL,
    params: _params,
  });
  return res;
};

//删除
export const deleteLink = async (params: string) => {
  const res = await upgradeHttp.delete<string>({
    url: Api.DELETE_LINK + '?routeLinkKey=' + params,
  });
  return res;
};
